const Router = require('../../Router');
const resContent = require('../../../../util/ResValue')
const Where = require('../../../../util/SqlWhere');
const dbUtil = require('../../../../util/dbutil');

class Weather extends Router {

    condition = [
        "id",
        "cityId",
        "fxDate",
        "sunrise",
        "sunset",
        "moonrise",
        "moonset",
        "moonPhase",
        "moonPhaseIcon",
        "tempMax",
        "tempMin",
        "iconDay",
        "textDay",
        "iconNight",
        "textNight",
        "wind360Day",
        "windDirDay",
        "windScaleDay",
        "windSpeedDay",
        "wind360Night",
        "windDirNight",
        "windScaleNight",
        "windSpeedNight",
        "humidity",
        "precip",
        "pressure",
        "vis",
        "cloud",
        "uvIndex",
    ];

    constructor() {
        //查询条件
        super('/super/weather', 'get', false);
    }

    async handle(ctx, next) {
        const body = ctx.request.query;
        const res = await this.dynamicWeatherSelect(body);
        ctx = await resContent.success(ctx);
        ctx.body.total = res.total;
        ctx.body.data = res.weather;
    }


    //动态天气查询
    async dynamicWeatherSelect(body) {
        //查询头
        let sqlHead = `
            SELECT
              weather.*,
              (SELECT JSON_OBJECT('id', id, 'name', name, 'fatherId', fatherId, 'level', level, 'code', code)
               FROM city
               WHERE id = weather.cityId
              ) AS city
            FROM
              weather
        `;
        //查询前缀
        let prefix = '';

        //动态查询参数
        let params = JSON.parse(JSON.stringify(this.condition));
        //如果有城市名的情况下
        if (body.name !== undefined) {
            sqlHead = `
                SELECT JSON_OBJECT(
                    'id', city.id,
                    'name', city.name,
                    'fatherId', city.fatherId,
                    'level', city.level,
                    'code', city.code
                ) AS city, weather.*
                FROM city
                JOIN weather ON city.id = weather.cityId
            `;
            //添加查询头
            prefix = 'city.';
            params.push('%name%');
        }

        //新建天气查询
        const whereNew = new Where(sqlHead, body);
        //设置查询前缀
        whereNew.setQueryConditionPrefix(prefix);
        //添加分页
        whereNew.setPagination(true);
        //设置动态
        whereNew.addQueryCriteriaMultiple(params);
        //查询天气
        let weather = await whereNew.executeTheQuery();
        //去除自定义命令
        whereNew.customCommands = [];
        //设置获取总数
        sqlHead = `
            SELECT COUNT(*)
            FROM city
            JOIN weather ON city.id = weather.cityId
        `;
        //重写查询头
        whereNew.setQueryHeader(sqlHead);
        //查询总数
        const total = await whereNew.executeTheQuery();

        //内部数据转成json
        weather = weather.map(item => {
            item.city = JSON.parse(item.city);
            item.day7Info = JSON.parse(item.day7Info);
            return item;
        });

        return {
            total: total.length === 0 ? 0 : total[0]["COUNT(*)"],
            weather: weather
        }
    }
}


module.exports = Weather;
